package com.quzhi.sys.controller.warehouseController;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quzhi.common.Result;
import com.quzhi.sys.dto.AddGoodsDto;
import com.quzhi.sys.dto.GoodsRecordDto;
import com.quzhi.sys.dto.InventoryDto;
import com.quzhi.sys.entity.*;
import com.quzhi.sys.service.impl.AddGoodsServiceImpl;
import com.quzhi.sys.service.impl.GoodsRecordServiceImpl;
import com.quzhi.sys.service.impl.InventoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xibing
 * @since 2024-07-30
 */

@Slf4j
@RestController
@RequestMapping("/sys/goodsRecord")
public class GoodsRecordController {

    @Autowired
    private GoodsRecordServiceImpl goodsRecordService;

    @Autowired
    private AddGoodsServiceImpl addGoodsService;

    @Autowired
    private InventoryServiceImpl inventoryService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/list")
    public Result<?> getInventoryList(
            @RequestParam(value = "productId") String productId,
            @RequestParam(value = "pageNo") Long pageNo,
            @RequestParam(value = "pageSize") Long pageSize
    ) {

        LambdaQueryWrapper<GoodsRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsRecord::getProductId,productId);
        Page<GoodsRecord> infoPage = new Page<>(pageNo, pageSize);
        goodsRecordService.page(infoPage, lambdaQueryWrapper);

        List<GoodsRecord> records = infoPage.getRecords();

        List<GoodsRecordDto> recordDtoList = new ArrayList<>();


        // 获取所有序列号
        List<Inventory> inventories = inventoryService.list();

        for (GoodsRecord record : records) {
            GoodsRecordDto goodsRecordDto = new GoodsRecordDto();

            List<AddGoodsDto> addGoodsDtos = new ArrayList<>();
            LambdaQueryWrapper<AddGoods> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AddGoods::getTableNumber, record.getTableNumber());
            List<AddGoods> list = addGoodsService.list(wrapper);
            for (AddGoods goods : list) {
                AddGoodsDto addGoodsDto = new AddGoodsDto();
                BeanUtils.copyProperties(goods, addGoodsDto);
                for (Inventory inventory : inventories) {
                    if(inventory.getId().equals(addGoodsDto.getInventoryId())){
                        addGoodsDto.setSerialNumber(inventory.getSerialNumber());
                        break;
                    }
                }
                addGoodsDtos.add(addGoodsDto);
            }

            BeanUtils.copyProperties(record, goodsRecordDto);
            goodsRecordDto.setAddGoods(addGoodsDtos);

            recordDtoList.add(goodsRecordDto);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("rows", recordDtoList);
        data.put("total", infoPage.getTotal());

        return Result.success(data);
    }



    // 更具表单删除
    @DeleteMapping("/{id}")
    public Result<?> deleteWareHouse(@PathVariable("id") String id, @RequestBody String qz_token) {
        // 查询当前用户的权限是否可以删除仓库
        Object obj = redisTemplate.opsForValue().get(qz_token);
        if (obj != null) {
            // 将数据转成json数据
            User loginUser = JSON.parseObject(JSON.toJSONString(obj), User.class);
            // 删除仓库
            String role = loginUser.getRole();
            if (role.equals("admin")) {
                GoodsRecord byId = goodsRecordService.getById(id);
                LambdaQueryWrapper<AddGoods> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(AddGoods::getTableNumber, byId.getTableNumber());
                List<AddGoods> list = addGoodsService.list(wrapper);
                addGoodsService.remove(wrapper);

                for (AddGoods goods : list) {
                    LambdaQueryWrapper<Inventory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(Inventory::getId, goods.getInventoryId());
                    inventoryService.remove(lambdaQueryWrapper);
                }
                // 删除库存
                // 权限足够可以删除
                goodsRecordService.removeById(id);
                return Result.success("删除成功");
            } else {
                return Result.fail(20002, "你没有权限执行此操作");
            }
        } else {
            return Result.fail(20002, "删除失败");
        }
    }


}
